package com.invite.me.beans.adpater;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.graphics.Palette;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.invite.me.R;
import com.invite.me.beans.res.BlindDataFragmentBean;

import java.util.List;


public class BlindDataAdpater extends BaseQuickAdapter<BlindDataFragmentBean.DataBean,BaseViewHolder> {


    public BlindDataAdpater(int layoutResId, @Nullable List<BlindDataFragmentBean.DataBean> data) {
        super(layoutResId, data);
    }


    @Override
    protected void convert(final BaseViewHolder helper, BlindDataFragmentBean.DataBean item) {


                if(item.getVo().getMyTagList()!=null&&item.getVo().getMyTagList().size()>0){
                    helper.setText(R.id.blindname,item.getVo().getMyTagList().get(0).getTitle());
                }

        helper .setText(R.id.price,item.getVo().getPrice()+"")
                .setText(R.id.namexianshi,item.getVo().getNickname())
                .setText(R.id.mishu,item.getDistance()+"")

                .addOnClickListener(R.id.yaoyue);

        RequestOptions options = new RequestOptions()
                .placeholder(R.mipmap.homezw);
//        Glide.with(mContext)
//                .load(item.getUil())
//                .apply(options)
//                .apply(RequestOptions.bitmapTransform(new RoundedCorners(10)))//圆角半径
//                .into((ImageView) helper.getView(R.id.hlindimage));
        Glide.with(mContext)
                .load(item.getVo().getDiscribe())
                .apply(options)
                .apply(RequestOptions.bitmapTransform(new RoundedCorners(8)))//圆角半径
                .into(new SimpleTarget<Drawable>() {
                    @Override
                    public void onResourceReady(@NonNull final Drawable resource, @Nullable Transition<? super Drawable> transition) {
                        ImageView view = helper.getView(R.id.hlindimage);
                        view.setImageDrawable(resource);
                        final TextView namexianshi= helper.getView(R.id.namexianshi);
                        if(getBitmap(view)!=null){

                            Palette.from(getBitmap(view)).generate(new Palette.PaletteAsyncListener() {
                                @Override
                                public void onGenerated(@Nullable Palette palette) {
                                    if (palette != null) {
                                        Palette.Swatch vibrant = palette.getMutedSwatch();//柔和的
                                        //  Palette.Swatch vibrant = palette.getDarkMutedSwatch();//柔和的暗色
                                        //  Palette.Swatch vibrant = palette.getDarkVibrantSwatch();//有活力的暗色
                                        //   Palette.Swatch vibrant = palette.getLightVibrantSwatch();//有活力的亮色
                                        //   Palette.Swatch vibrant = palette.getVibrantSwatch();//有活力的
                      /*  tvVibrant.setText("有活力的");
                        tvVibrantBurn.setText("有活力的-加深");*/
                                        if (vibrant != null) {
                                            //  tvVibrant.setBackgroundColor(vibrant.getRgb(), 0.3f);

                                            Resources resources=mContext.getResources();
                                            Drawable drawable=resources.getDrawable(R.drawable.blind_name_yuan);
                                            drawable.setColorFilter(setColorShallow(vibrant.getRgb(), 0.3f),PorterDuff.Mode.SRC_ATOP);
                                            namexianshi.setBackgroundDrawable(drawable);
                                            //  namexianshi.setBackgroundColor(setColorShallow(vibrant.getRgb(), 0.3f));


                                        }

                                    }
                                }
                            });
                        }


                    }
                });
    }

    private Bitmap getBitmap(ImageView imageView) {
        return ((BitmapDrawable) imageView.getDrawable()).getBitmap();
    }
    /**
     * 颜色浅化算法
     */
    private int setColorShallow(int rgb, float val) {
        int r = (rgb >> 16) & 0xff;
        int g = (rgb >> 8) & 0xff;
        int b = rgb & 0xff;
        r = (int) Math.floor(r * (1f + val));
        g = (int) Math.floor(g * (1f + val));
        b = (int) Math.floor(b * (1f + val));
        return Color.rgb(r, g, b);
    }

}
